import {
    clearLStorage,
    clearSStorage,
    getExpiresIn,
    getRefreshToken,
    getToken,
    removeExpiresIn,
    removeRefreshToken,
    removeToken,
    setExpiresIn,
    setRefreshToken,
    setToken
} from "@/utils/auth";
import {LoginAction} from "@/stores/actions/loginActions";
import {ERRORCODE} from "@/utils/constant";
import {HttpCodeEnum} from "@/enums/HttpCodeEnum";
import {message} from "antd";

export interface ILoginReducerState {
    access_token?: string
    refresh_token?: string
    expires_in?: number
    navigateToPath?: string
    login?: {
        loading: boolean
        success: boolean
        message: string
    }
    logout?: {
        loading: boolean
        success: boolean
        message: string
    }
}

const initialState: ILoginReducerState = {
    access_token: getToken() || '',
    refresh_token: getRefreshToken() || '',
    expires_in: Number(getExpiresIn()) || 0,
    navigateToPath: '',
    login: {
        loading: false,
        success: false,
        message: ""
    },
    logout: {
        loading: false,
        success: false,
        message: ""
    }
}

const loginReducer = (state: ILoginReducerState = initialState, action: LoginAction): ILoginReducerState => {
    switch (action.type) {
        case "LOGIN":
            return {
                ...state,
                login: {
                    loading: true,
                    success: false,
                    message: ''
                }
            }
        case "LOGINSUCCESS":
            setToken(action.payload.access_token)
            setRefreshToken(action.payload.access_token)
            setExpiresIn(String(action.payload.expires_in) || '0')
            return {
                ...state,
                access_token: action.payload.access_token,
                refresh_token: action.payload.access_token,
                expires_in: action.payload.expires_in,
                login: {
                    loading: false,
                    success: true,
                    message: ERRORCODE[HttpCodeEnum.SUCCESS]
                }
            }
        case "LOGINFAIL":
            return {
                ...state,
                login: {
                    loading: false,
                    success: false,
                    message: action.message
                }
            }
        case "LOGOUT":
            return {
                ...state,
                logout: {
                    loading: true,
                    success: false,
                    message: ""
                }
            }
        case "LOGOUTSUCCESS":
            clearLStorage()
            clearSStorage()
            removeToken()
            removeRefreshToken()
            removeExpiresIn()
            message.success('退出登录成功！')
            setTimeout(() => {
                window.location.replace(`${process.env.PUBLIC_URL}/login`)
            }, 0)
            console.log(action)
            return {
                ...state,
                logout: {
                    loading: false,
                    success: true,
                    message: ""
                }
            }
        case "LOGOUTFAIL":
            return {
                ...state,
                logout: {
                    loading: false,
                    success: false,
                    message: action.message
                }
            }
        default:
            return state
    }
}

export default loginReducer